package com.ruoyi.system.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ruoyi.common.utils.CommonUtil;
import com.ruoyi.common.utils.PageUtils;
import com.ruoyi.common.core.page.PagePlus;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.system.apiService.CodeService;
import com.ruoyi.system.domain.ZsyyQrCode;
import com.ruoyi.system.domain.ZsyyUser;
import com.ruoyi.system.mapper.ZsyyUserMapper;
import org.springframework.stereotype.Service;
import com.ruoyi.common.core.mybatisplus.core.ServicePlusImpl;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.ruoyi.system.bo.ZsyyCouponAddBo;
import com.ruoyi.system.bo.ZsyyCouponQueryBo;
import com.ruoyi.system.bo.ZsyyCouponEditBo;
import com.ruoyi.system.domain.ZsyyCoupon;
import com.ruoyi.system.mapper.ZsyyCouponMapper;
import com.ruoyi.system.vo.ZsyyCouponVo;
import com.ruoyi.system.service.IZsyyCouponService;
import com.ruoyi.system.mapper.ZsyyCouponMapper;

import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.Collection;
import org.springframework.beans.factory.annotation.Autowired;
/**
 * 优惠券管理Service业务层处理
 *
 * @author wangghua
 * @date 2021-07-14
 */
@Service
public class ZsyyCouponServiceImpl extends ServicePlusImpl<ZsyyCouponMapper, ZsyyCoupon> implements IZsyyCouponService {
    @Autowired
    private ZsyyCouponMapper iZsyyCouponMapper;
    @Autowired
    private ZsyyUserMapper userMapper;

	private String APIKEY="wx4250bfd6f75fd83c";//小程序id
	private String SECRETKEY="0417653972a45ba7ed2f8a2a4a166775";//小程序密钥

    @Override
    public ZsyyCouponVo queryById(Long id){
        return getVoById(id, ZsyyCouponVo.class);
    }

    @Override
    public TableDataInfo<ZsyyCouponVo> queryPageList(ZsyyCouponQueryBo bo) {
		Page<ZsyyCouponVo> page = iZsyyCouponMapper.queryPageList(PageUtils.buildPage(),bo);
		return PageUtils.buildDataInfo(page);
    }

    @Override
    public List<ZsyyCouponVo> queryList(ZsyyCouponQueryBo bo) {
        return listVo(buildQueryWrapper(bo), ZsyyCouponVo.class);
    }

    private LambdaQueryWrapper<ZsyyCoupon> buildQueryWrapper(ZsyyCouponQueryBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<ZsyyCoupon> lqw = Wrappers.lambdaQuery();
        lqw.eq(StrUtil.isNotBlank(bo.getTitle()), ZsyyCoupon::getTitle, bo.getTitle());
        return lqw;
    }

    @Override
    public Boolean insertByAddBo(ZsyyCouponAddBo bo) {
		if (bo.getCourseId() == 3){
			ZsyyUser userByPhone = userMapper.getUserByPhone(bo.getPhone());
			if (!CommonUtil.isEmpty(userByPhone)){
				bo.setUserId(userByPhone.getId());
			}
		}
        ZsyyCoupon add = BeanUtil.toBean(bo, ZsyyCoupon.class);
		add.setOnlyCode(1);
		add.setSurplus(bo.getTotal());
        validEntityBeforeSave(add);
		save(add);
		if (bo.getCourseId() == 3){
			String path = createCode(add.getId());
			add.setUrl(path);
			updateById(add);
		}

        return true;
    }

    @Override
    public Boolean updateByEditBo(ZsyyCouponEditBo bo) {
		if (bo.getCourseId() == 3){
			ZsyyUser userByPhone = userMapper.getUserByPhone(bo.getPhone());
			if (!CommonUtil.isEmpty(userByPhone)){
				bo.setUserId(userByPhone.getId());
			}
		}
        ZsyyCoupon update = BeanUtil.toBean(bo, ZsyyCoupon.class);
		update.setOnlyCode(1);
		update.setUpdatetime(new Date());
        validEntityBeforeSave(update);
        return updateById(update);
    }

    /**
     * 保存前的数据校验
     *
     * @param entity 实体类数据
     */
    private void validEntityBeforeSave(ZsyyCoupon entity){
        //TODO 做一些数据校验,如唯一约束
    }

    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return removeByIds(ids);
    }

	/*生成优惠卷二维码*/
	/**
	 * 生成二维码方法
	 * @return
	 */
	public String createCode(Long id){
		String accessToken = null;
		ZsyyQrCode qrCode = new ZsyyQrCode();
		qrCode.setId(id);
		String path = "";
		try{
			accessToken = CodeService.getAccessToken(APIKEY,SECRETKEY);
			path = CodeService.addWxCode(accessToken,qrCode,"pages/proper/coupon");
			return path;
		}catch (Exception e){
			e.printStackTrace();
		}
		return path;
	}
}
